package com.sugar.subscribe.course.module.business.controller.admin.coach;

import com.sugar.subscribe.course.module.business.controller.app.serviceclass.vo.AppServiceClassRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coach.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coach.CoachDO;
import com.sugar.subscribe.course.module.business.service.coach.CoachService;

@Tag(name = "管理后台 - 教练信息")
@RestController
@RequestMapping("/business/coach")
@Validated
public class CoachController {

    @Resource
    private CoachService coachService;

    @PostMapping("/create")
    @Operation(summary = "创建教练信息")
    @PreAuthorize("@ss.hasPermission('business:coach:create')")
    public CommonResult<Long> createCoach(@Valid @RequestBody CoachSaveReqVO createReqVO) {
        return success(coachService.createCoach(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教练信息")
    @PreAuthorize("@ss.hasPermission('business:coach:update')")
    public CommonResult<Boolean> updateCoach(@Valid @RequestBody CoachSaveReqVO updateReqVO) {
        coachService.updateCoach(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/amount")
    @Operation(summary = "更新资金信息")
    @PreAuthorize("@ss.hasPermission('business:coach:update')")
    public CommonResult<Boolean> updateCoachAmount(@Valid @RequestBody CoachAmountUpdateReqVO amountUpdateReqVO) {
        coachService.updateCoachAmount(amountUpdateReqVO);
        return success(true);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核教练信息")
    @PreAuthorize("@ss.hasPermission('business:coach:update')")
    public CommonResult<Boolean> auditCoach(@Valid @RequestBody CoachAuditReqVO auditReqVO) {
        coachService.auditCoach(auditReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:coach:delete')")
    public CommonResult<Boolean> deleteCoach(@RequestParam("id") Long id) {
        coachService.deleteCoach(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教练信息")
                @PreAuthorize("@ss.hasPermission('business:coach:delete')")
    public CommonResult<Boolean> deleteCoachList(@RequestParam("ids") List<Long> ids) {
        coachService.deleteCoachListByIds(ids);
        return success(true);
    }

    @GetMapping("/all")
    @Operation(summary = "获得教练信息")
    @PreAuthorize("@ss.hasPermission('business:coach:query')")
    public CommonResult<List<CoachSimpleRespVO>> getAllCoach() {
        return success(coachService.getAllCoach());
    }

    @GetMapping("/get")
    @Operation(summary = "获得所有教练信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:coach:query')")
    public CommonResult<CoachDetailRespVO> getCoachDetail(@RequestParam("id") Long id) {
        return success(coachService.getCoachDetail(id));
    }

    @GetMapping("/get/service/class/{coachId}")
    @Operation(summary = "获得教练下的服务分类信息（列表）")
    public CommonResult<List<AppServiceClassRespVO>> getCoachServiceClassListByCoachId(@PathVariable("coachId") Long coachId) {
        return success(coachService.getCoachServiceClassListByCoachId(coachId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练信息分页")
    @PreAuthorize("@ss.hasPermission('business:coach:query')")
    public CommonResult<PageResult<CoachRespVO>> getCoachPage(@Valid CoachPageReqVO pageReqVO) {
        PageResult<CoachDO> pageResult = coachService.getCoachPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CoachRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:coach:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachExcel(@Valid CoachPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoachDO> list = coachService.getCoachPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教练信息.xls", "数据", CoachRespVO.class,
                        BeanUtils.toBean(list, CoachRespVO.class));
    }

}